<?php

namespace App\Http\Services\MessageFetch\FetchWorker;

use Mockery\Exception;
use App\Common\Tools\RequestTools;
use App\Http\Services\MessageFetch\BaseMsgFetchHandle;

class DailyWoolWorker extends BaseMsgFetchHandle
{
    protected string $noticeType = 'timing_fleece';

    private String $txt = '';

    /**
     * 发送信息.
     *
     * @return void
     *
     * @throws \Throwable
     */
    protected function fetchUrlContent()
    {
        try {
            $this->fetchSuperXb();
        } catch (Exception $exception) {
            throw_if_str(true, '请求发送信息异常' . $exception->getMessage());
        }
    }

    private function fetchQingFeng()
    {
        $idPath    = storage_path('app/qingfeng_id.txt');
        $id        = file_get_contents($idPath);
        $lastId    = 0;

        for ($i = 1; $i <= 4; ++$i) {
            $this->url = sprintf(config('wechat.wx_work.wool_qingfeng_url'), $id + $i);
            $content   = RequestTools::make()->request('get', $this->url);
            $content   = substr($content[0], 200, 700);
            preg_match("/<title>(.*?)<\/title>/i", $content, $matches);
            $title = $matches[1] ?? '';
            if (!$title) {
                break;
            }

            //组装完整的提示信息
            $prefix    = $this->txt ? PHP_EOL . PHP_EOL : '';
            $this->txt .= $prefix . str_replace(' - 清风线报', '', $title) . PHP_EOL . $this->url;

            //记录最后的id
            $lastId = $id + $i;
        }
        $lastId && file_put_contents($idPath, $lastId);
    }

    private function fetchSuperXb()
    {
        $idPath    = storage_path('app/super_id.txt');
        $id        = intval(file_get_contents($idPath));
        $lastId    = 0;

        $this->url  = sprintf(config('wechat.wx_work.wool_super_url'), $id + 1);
        $content    = RequestTools::make()->request('get', $this->url);
        $res        = [];
        $countLimit = 10;
        foreach ($content['data'] as $key => $datum) {
            if ($key >= $countLimit) {
                break;
            }

            preg_match('/ data-tooltip="(.*?)"/i', $datum, $matches);
            $title = $matches[1] ?? '';
            $title = str_replace(['[图]', '网页链接', '京红', '超红'], '', $title);
            if (empty($title)) {
                continue;
            }

            preg_match('/ data-id="(.*?)"/i', $datum, $match);
            $cid   = $match[1] ?? '';
            $res[] = ['id' => $cid, 'title' => trim($title)];
        }

        $lastId = current($res)['id'] ?? $id;

        foreach ($res as $re) {
            //组装完整的提示信息
            $prefix    = $this->txt ? PHP_EOL . PHP_EOL : '';
            $this->txt .= $prefix . $re['title'] . '  https://cjx8.com/post/' . $re['id'];
        }

        //记录最后的id
        $lastId && file_put_contents($idPath, $lastId);
    }

    public function fetchKcn()
    {
        $idPath    = storage_path('app/kcn_id.txt');
        $id        = intval(file_get_contents($idPath));

        $this->url  = 'https://907k.cn/';
        $content    = RequestTools::make()->request('get', $this->url);
        $pattern    = '/<a\s+href="\/xb\/(\d+)\.html"[^>]*>\s*<h4>([^<]+)<\/h4>\s*<\/a>/';
        preg_match_all($pattern, $content[0], $matches, PREG_SET_ORDER);

        $lastId = 0;
        foreach ($matches as $key => $match) {
            $resItem = [
                'href' => "/xb/{$match[1]}.html",
                'id'   => (int) $match[1],
                'text' => trim($match[2]),
            ];
            0 == $key && $lastId = $resItem['id'];
            if ($resItem['id'] > $id) {
                $prefix    = $this->txt ? PHP_EOL . PHP_EOL : '';
                $this->txt .= $prefix . $resItem['text'] . '  https://907k.cn' . $resItem['href'];
            }
        }

        //记录最后的id
        $lastId && file_put_contents($idPath, $lastId);
    }

    protected function afterLogic()
    {
        if (!empty($this->txt)) {
            $this->pushToWxMsg($this->txt);
        }
    }
}
